home *** CD-ROM | disk | FTP | other *** search
/ Aminet 48 / Aminet 48 (2002)(GTI - Schatztruhe)[!][Apr 2002].iso / Aminet / util / moni / Scout-src.lha / source / objects / scout_commands.c < prev    next >
Encoding:
C/C++ Source or Header  |  2002-02-13  |  8.7 KB  |  296 lines

  1. /**
  2.  * Scout - The Amiga System Monitor
  3.  *
  4.  *------------------------------------------------------------------
  5.  *
  6.  * This program is free software; you can redistribute it and/or modify
  7.  * it under the terms of the GNU General Public License as published by
  8.  * the Free Software Foundation; either version 2 of the License, or
  9.  * any later version.
  10.  *
  11.  * This program is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  * GNU General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU General Public License
  17.  * along with this program; if not, write to the Free Software
  18.  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  *
  20.  * You must not use this source code to gain profit of any kind!
  21.  *
  22.  *------------------------------------------------------------------
  23.  *
  24.  * @author Andreas Gelhausen
  25.  * @author Richard Körber <rkoerber@gmx.de>
  26.  */
  27.  
  28.  
  29.  
  30. #include "system_headers.h"
  31.  
  32. int  comcnt;
  33.  
  34. static APTR ComPool = NULL;
  35.  
  36. char * GetUCType (LONG uc) {
  37.    char *UCTypeText[] = {
  38.       "SYSTEM","INTERNAL","DISABLED"
  39.    };
  40.  
  41.    LONG UCType[] = {
  42.       CMD_SYSTEM,CMD_INTERNAL,CMD_DISABLED,0
  43.    };
  44.  
  45.    int i = 0;
  46.  
  47.    while (UCType[i]) {
  48.       if (uc == UCType[i]) {
  49.          return (UCTypeText[i]);
  50.       }
  51.       i++;
  52.    }
  53.    return (NULL);
  54. }
  55.  
  56. __asm __saveds LONG comlist_dspfunc(register __a2 char **array, register __a1 struct CommandEntry *comentry, register __a0 struct Hook *hook)
  57. {
  58.    if (comentry) {
  59.       *array++ = comentry->cm_address;
  60.       *array++ = comentry->cm_name;
  61.       *array++ = comentry->cm_uc;
  62.       *array++ = comentry->cm_lower;
  63.       *array++ = comentry->cm_upper;
  64.       *array++ = comentry->cm_size;
  65.       *array   = NULL;
  66.    } else {
  67.       *array++ = ESC "bAddress";
  68.       *array++ = ESC "bName";
  69.       *array++ = ESC "bUseCount";
  70.       *array++ = ESC "b" ESC "cLower";
  71.       *array++ = ESC "b" ESC "cUpper";
  72.       *array++ = ESC "bSize";
  73.       *array   = NULL;
  74.    }
  75.    return(0);
  76. }
  77.  
  78. struct Hook comlist_dsphook = {
  79.  {NULL, NULL},
  80.  (ULONG (* )())comlist_dspfunc,
  81.  NULL, NULL
  82. };
  83.  
  84. void FreeCommands (void)
  85. {
  86.     MyFreePoolStructs(&ComPool, NULL, NULL, comlist);
  87. }
  88.  
  89. int GetCommands (struct CommandEntry **first) {
  90.    struct   Segment        *segment;
  91.    struct   CommandEntry   *comentry,*previous = NULL;
  92.    long     *seg, size;
  93.    char     *tmp;
  94.  
  95.    int comcnt = 0;
  96.    *first = 0;
  97.  
  98.    if (!ComPool) ComPool = tbCreatePool(MEMF_CLEAR, 4096, 4096);
  99.  
  100.    if (clientstate) {
  101.       if (SendDaemon ("GetComList")) {
  102.          while ((comentry = tbAllocPooled(ComPool, sizeof(struct CommandEntry))) \
  103.            && (ReceiveDecodedEntry ((UBYTE *) comentry, sizeof (struct CommandEntry)))) {
  104.             IsHex (comentry->cm_address, (long *) &comentry->cm_adr);
  105.  
  106.             if (! *first)
  107.                *first = comentry;
  108.             if (previous)
  109.                previous->cm_next = comentry;
  110.  
  111.             comcnt++;
  112.             previous = comentry;
  113.          }
  114.       }
  115.    } else {
  116.       segment = (struct Segment *) BADDR(DEVINFO->di_NetHand);
  117.       while ((segment) && (comentry = tbAllocPooled(ComPool, sizeof(struct CommandEntry)))) {
  118.          if (! *first)
  119.             *first = comentry;
  120.          if (previous)
  121.             previous->cm_next = comentry;
  122.    
  123.          comentry->cm_adr = (char *) segment;
  124.          _sprintf (comentry->cm_address, "$%08lx", segment);
  125.          strncpy (comentry->cm_name, (char *) &segment->seg_Name + 1, (UBYTE) segment->seg_Name[0]);
  126.    
  127.          if (tmp = GetUCType (segment->seg_UC)) {
  128.             strcpy (comentry->cm_uc, tmp);
  129.    
  130.             strcpy (comentry->cm_lower, ESC "c---");
  131.             strcpy (comentry->cm_upper, ESC "c---");
  132.             strcpy (comentry->cm_size, ESC "c---");
  133.    
  134.             comcnt++;
  135.             previous = comentry;
  136.          } else {
  137.             _sprintf (comentry->cm_uc, "%ld", segment->seg_UC - 1);
  138.    
  139.             seg = BADDR(segment->seg_Seg);
  140.             comcnt++;
  141.             while (seg) {
  142.                _sprintf (comentry->cm_lower, "$%08lx", ((char *) seg) + 4);
  143.                size = *(seg - 1);
  144.                _sprintf (comentry->cm_size, "%ld", size);
  145.                _sprintf (comentry->cm_upper, "$%08lx", ((char *) seg) - 4 + size);
  146.    
  147.                previous = comentry;
  148.                if (seg = BADDR(*seg)) {
  149.                   comentry = tbAllocPooled(ComPool, sizeof(struct CommandEntry));
  150.                   if (previous)
  151.                      previous->cm_next = comentry;
  152.                   comentry->cm_address[0] = ' ';
  153.                }
  154.             }
  155.          }
  156.          segment = (struct Segment *) BADDR(segment->seg_Next);
  157.       }
  158.    }
  159.    return (comcnt);
  160. }
  161.  
  162. void PrintCommands (char *filename) {
  163.    int i=1;
  164.    BPTR handle;
  165.    struct CommandEntry *entryp = NULL;
  166.  
  167.    handle = HandlePrintStart (filename);
  168.    if ((handle) && (PrintOneLine (handle, "\n  Address  Name            UseCount     Lower     Upper    Size\n\n"))) {
  169.       if (! WI_Commands) {
  170.          i = GetCommands (&entryp);
  171.       }
  172.       if (i) {
  173.          for (i=0;;i++) {
  174.             if (WI_Commands)
  175.                DoMethod (comlist,MUIM_List_GetEntry,i,&entryp);
  176.             if (!entryp) break;
  177.  
  178.             _sprintf (tmpstr2, " %9s %-15.15s %8s", entryp->cm_address, entryp->cm_name, entryp->cm_uc);
  179.             if ((entryp->cm_uc[0] == 'I') || (entryp->cm_uc[0] == 'S') || (entryp->cm_uc[0] == 'D')) {
  180.                _sprintf (tmpstr, " %9s %9s %7s\n", entryp->cm_lower+2, entryp->cm_upper+2, entryp->cm_size+2);
  181.                strcat (tmpstr2, tmpstr);
  182.             } else {
  183.                _sprintf (tmpstr, " %9s %9s %7s\n", entryp->cm_lower, entryp->cm_upper, entryp->cm_size);
  184.                strcat (tmpstr2, tmpstr);
  185.             }
  186.             if (! (PrintOneLine (handle, tmpstr2)))
  187.                break;
  188.  
  189.             if (! WI_Commands)
  190.                entryp = entryp->cm_next;
  191.          }
  192.       }
  193.    }
  194.    HandlePrintStop();
  195. }
  196.  
  197. void ShowCommands (void) {
  198.    struct   CommandEntry   *com;
  199.  
  200.    ApplicationSleep();
  201.    set (comlist,MUIA_List_Quiet,TRUE);
  202.    set (comlist,MUIA_List_CompareHook,comlist_cmphook_ptr);
  203.    set (BT_ComRemove, MUIA_Disabled, TRUE);
  204.  
  205.    FreeCommands();
  206.    comcnt = GetCommands (&com);
  207.  
  208.    while (com) {
  209.       InsertSortedEntry (comlist, (APTR *) &com);
  210.       com = com->cm_next;
  211.    }
  212.  
  213.    SetCountText (comcount, comcnt);
  214.    AwakeApplication();
  215.    set (comlist,MUIA_List_Quiet,FALSE);
  216. }
  217.  
  218. void SendComList (void) {
  219.    struct   CommandEntry   *com;
  220.  
  221.    FreeCommands();
  222.    comcnt = GetCommands (&com);
  223.  
  224.    while (com) {
  225.       SendEncodedEntry ((UBYTE *) com, sizeof (struct CommandEntry));
  226.       com = com->cm_next;
  227.    }
  228.    FreeCommands();
  229. }
  230.  
  231.  
  232. APTR WI_Commands, comlist, comtext, comcount, CY_ComSort;
  233. int  comsortstate = 0;
  234. APTR BT_ComUpdate, BT_ComPrint, BT_ComRemove, BT_ComExit;
  235.  
  236. APTR ComSortList[] = {
  237.    &list_cmpnormalhook,
  238.    &list_cmpnamehook,
  239.    &list_cmpaddresshook,
  240.    NULL
  241. };
  242.  
  243. static const char *CYA_ComSortText[] = {
  244.    "normal",
  245.    "name",
  246.    "address",
  247.    NULL
  248. };
  249.  
  250. char commands_title[WINDOWTITLELEN];
  251.  
  252. void CommandsWindow (BOOL state) {
  253.    if (state) {
  254.       if (WI_Commands) {
  255.          ShowCommands();
  256.       } else {
  257.          WI_Commands = WindowObject,
  258.          MUIA_Window_Title, MyGetWindowTitle (commands_title, "RESIDENT COMMANDS"),
  259.          MUIA_HelpNode, CommandsText,
  260.          MUIA_Window_ID, MakeListID('R','C','O','M'),
  261.          WindowContents, VGroup,
  262.             Child, comlist = MyListviewObject ("COL=0 DELTA=8,COL=1 DELTA=8,COL=2 DELTA=8 P=\33c,COL=3 DELTA=8,COL=4 DELTA=8,COL=5 P=\33r",&comlist_dsphook),
  263.             Child, MyBelowSortedListview (&comtext, &comcount, &CY_ComSort, CYA_ComSortText, comsortstate),
  264.             Child, MyVSpace(2),
  265.             Child, HGroup, MUIA_Group_SameSize, TRUE,
  266.                Child, BT_ComUpdate = KeyButtonA (UpdateText,ID_COMUPDATE),
  267.                Child, BT_ComPrint  = KeyButtonA (PrintText ,ID_COMPRINT),
  268.                Child, BT_ComRemove = KeyButtonA (RemoveText,ID_COMREMOVE),
  269.                Child, BT_ComExit   = KeyButtonA (ExitText  ,ID_COMEXIT),
  270.             End,
  271.          End, End;
  272.  
  273.          DoMethod (AP_Scout,OM_ADDMEMBER,WI_Commands);
  274.          DoMethod (WI_Commands,MUIM_Window_SetCycleChain,comlist,CY_ComSort,BT_ComUpdate,BT_ComPrint,BT_ComRemove,BT_ComExit,NULL);
  275.          DoMethod (CY_ComSort, MUIM_Notify, MUIA_Cycle_Active, MUIV_EveryTime, AP_Scout, 2, MUIM_Application_ReturnID, ID_COMSORT);
  276.  
  277.          SetCloseRequest (WI_Commands,ID_COMEXIT);
  278.          SetListActive (comlist,ID_COMLV_ACTIVE);
  279.  
  280.          ShowCommands();
  281.  
  282.          SetWindowOpen (WI_Commands,comlist,ID_COMEXIT);
  283.       }
  284.    } else if ((! state) && (WI_Commands)) {
  285.       SetWindowClose (WI_Commands,TRUE);
  286.  
  287.       FreeCommands();
  288.  
  289.       DoMethod (AP_Scout,OM_REMMEMBER,WI_Commands);
  290.       MUI_DisposeObject (WI_Commands);
  291.       WI_Commands = NULL;
  292.       comlist = NULL;
  293.    }
  294. }
  295.  
  296.